package dsmflex.taskengine;
import java.sql.Statement;
import java.sql.Connection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.liusy.jdbc.JdbcHelp;
import com.liusy.taskengine.executemodel.DBExecuteModel;
import com.liusy.taskengine.taskexecute.AbstractTaskExecute;
import com.liusy.taskengine.taskexecute.ITaskExecute;
import com.liusy.taskengine.taskmodel.ITaskModel;


/**
 * 批量执行sql的具体执行者
 * 
 * @author liusy 2012-12-11
 */
public class SqlExecuteModel extends DBExecuteModel {
	
	private static Logger log = Logger.getLogger(SqlExecuteModel.class);
	
	private Connection resultConn = null;
			private  Statement  results = null;
	
	public SqlExecuteModel() {
	}

	public SqlExecuteModel(String executeModelName) {
		super(executeModelName);
	}

	@Override
	public  List<Map<String,Object>> DBTask(ITaskModel task) {
		AbstractTaskExecute _taskExecute = (AbstractTaskExecute) taskExecute;
		SqlTaskModel sqlTaskModel = ((SqlTaskModel)taskModel);
		Map<String,Object> rowMap = sqlTaskModel.getRowMap();
		
		try {
			List<Map<String,Object>> result=JdbcHelp.getDataObject(sqlTaskModel.getSql(), this.s);
			if(result.size()>0)
			{										  
				System.out.println(result.get(0).get("CUSTOMERID")+"  "+rowMap.get("name"));
			}else
				System.out.println("无");
			//log.info(result);
//			//select distinct ml.meter_id,val,load_type,runtime from  trans_dc_market_customer_new tdc,
//			log.info(sqlTaskModel.getSql());
//			if(list.size()>0)
//			{
//				//Map<String,Object> map = list.get(0);
//				float maxload= convertFloat(rowMap.get("MAXLOAD"));
//				float retainload= convertFloat(rowMap.get("RETAINLOAD"));
//				float ugrency  = 0;
//				if(maxload>retainload)
//					ugrency= maxload-retainload;
//		
//
//				if(null ==results||null ==resultConn)
//				{
//					resultConn = JdbcHelp.getJdbcHelp().getConnection("REAULTCONN");
//					results = resultConn.createStatement();
//				}
//				for (Map<String, Object> map : list) {
//					float realload= convertFloat(map.get("VAL"));
//					float alreadyload = 0;
//					if(ugrency>retainload)
//						alreadyload= ugrency-realload;
//					String sql = "insert into day_compare_result values (day_compare_result_id_seq.nextval,'"+rowMap.get("CONSUMERID")+
//							"','"+rowMap.get("CONSUMERNAME")+"','"+map.get("METER_ID")+"','"+maxload+"','"+
//							retainload+"','"+ugrency+"','"+realload+"','"+alreadyload+"','"+map.get("RUNTIME")+"',sysdate,'','"+
//							rowMap.get("SUBSTATIONID")+"','"+rowMap.get("SUBSTATIONNAME")+"','"+rowMap.get("LINEID")+"','"+rowMap.get("LINENAME")+"','"+
//							rowMap.get("TRANSFORMERID")+"','"+rowMap.get("TRANSFORMERNAME")+"','"+rowMap.get("ZONEID")+"')";
//					results.addBatch(sql);
//					
//					log.info(sql);
//				}
//				int [] reutl = results.executeBatch();
//				log.info("executeBatch结果："+Arrays.toString(reutl));
//			}
			return result;
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	} 

	public float convertFloat(Object obj)
	{
		if(null==obj||obj.equals("")||obj.equals("null"))
		{
			return 0;
		}
		try {
			return  Float.parseFloat(obj.toString());
		} catch (Exception e) {
			return 0;
		}
	}
}
